﻿@page "/NginxInfo"
<h3>Nginx信息</h3>

<MButton OnClick="OnCheckNginx">查看Nginx状态</MButton>

@* @nginxStatus *@

<MTreeview @bind-Open="@initiallyOpen" @bind-active="@selectedDirList"
Light
TItem="DirectoryNode"
TKey="string"
Items="directoryNodeViewModel.directoryNode.Children"
Activatable
ItemKey="u => u.FullPath"
ItemChildren="u => u.IsDir?u.Children:null"
ItemText="u => u.Name"
LoadChildren="LoadDirectoryTree">
    <PrependContent>
        @if (context.Item.IsDir)
        {
            <MIcon>
                @(context.Open ? "mdi-folder-open" : "mdi-folder")
            </MIcon>
        }
        else
        {
            <MIcon>
                @("mdi-language-html5")
            </MIcon>
        }
    </PrependContent>
    <LabelContent Context="item">
        <div style="display: flex; align-items: center; justify-content: space-between;">
            <span>@item.Item.Name</span>
            @if (@item.Item.Name.EndsWith(".conf"))
            {
                <MButton Color="primary" Small OnClick="() => EditConfig(item.Item)">
                    编辑配置
                </MButton>
            }
        </div>
    </LabelContent>
</MTreeview>

<PDrawer @bind-Value="@nginxContextVisible" Title="编辑" Persistent>
    <NginxConfigEditor Config="nginxConfig" OnSave="SaveNginxConfig" OnCancel="CancelNginxConfig" />
</PDrawer>


@code {
    private bool nginxContextVisible = false;

    private NginxServerConfig nginxConfig = new NginxServerConfig();
    private List<string> _selectedDirList = new List<string>();
    private List<string> selectedDirList
    {
        get => _selectedDirList; set
        {
            if (value != _selectedDirList)
            {
                _selectedDirList = value;
                selectedDir = directoryNodeViewModel.FindDirectoryNode(value.FirstOrDefault());
            }
        }
    }
    /// <summary>
    /// 选择中的路径
    /// </summary>
    private DirectoryNode _selectedDir;
    private DirectoryNode selectedDir
    {
        get => _selectedDir;
        set
        {
            if (value != _selectedDir)
            {
                _selectedDir = value;
                if (value == null)
                {
                    return;
                }
                if (value.IsDir)
                {
                    uploadDir = value.FullPath;
                }
                else
                {
                    uploadDir = Path.GetDirectoryName(value.FullPath);
                }
            }
        }
    }
    /// <summary>
    /// 上传路径
    /// </summary>
    private string uploadDir;

    private string editPath;

    private bool contextMenuVisible = false;

    List<string> initiallyOpen = new() { "public" };
    [Inject]
    private ISshClientService sshClientService { get; set; }
    [Inject]
    public IRemoteNginxService RemoteNginxService { get; set; }
    /// <summary>
    /// Nginx状态
    /// </summary>
    private string nginxStatus = string.Empty;
    private DirectoryNodeViewModel directoryNodeViewModel = new DirectoryNodeViewModel() { directoryNode = new DirectoryNode() { Name = "/etc/nginx", IsDir = true, FullPath = "/etc/nginx" } };

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            LoadDirectoryTree(directoryNodeViewModel.directoryNode);
        }
    }

    private async Task OnCheckNginx()
    {
        var result = sshClientService.ExecuteCommand(new NginxCommand(){CommandType=NginxCommandTypeEnum.Status});
        nginxStatus = result.Output;
    }

    private void EditConfig(DirectoryNode directoryNode)
    {
        editPath = directoryNode.FullPath;
        nginxConfig = RemoteNginxService.GetCurrentConfig(directoryNode.FullPath);
        nginxContextVisible = true;
    }

    private async Task LoadDirectoryTree(DirectoryNode directoryNode)
    {
        await sshClientService.LoadDirectoryTree(directoryNode);
        StateHasChanged();
    }

    private async Task SaveNginxConfig(NginxServerConfig config)
    {
        await RemoteNginxService.UpdateConfig(config, editPath);
    }

    private void CancelNginxConfig()
    {
        nginxContextVisible = false;
    }
}
